Declarative way for setting MongoDB transaction options #4552
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #1628
After I read the issue description and thought about it a bit, I figured there's conceptually three possible approaches to implement requested functionality:
@Transactional
annotations like@Transactional(readPreference = PRIMARY)
@Transactional
First option requires to add database-specific attributes to database-generic annotation. That doesn't sound right.
Second option can be implemented either by completely re-implementing/duplicating whole
@Transactional
functionality in Spring Data MongoDb or by altering@Transactional
interceptor so that it can pass attributes of new annotation along with existing values. Even if that's possible, it will be too much effort. Also, splitting transaction configuration into two annotations kinda confusing.So I implemented third option. The only suitable attribute in
@Transactional
to store MongoDb options islabel
. Spring docs suggest that:This PR adds possibility to add 4 labels that will be recognized as transaction options on per-
@Transactional
basis:mongo:maxCommitTime={value}
mongo:readConcern={value}
mongo:readPreference={value}
mongo:writeConcern={value}
mongo:readConcern
,mongo:readPreference
,mongo:writeConcern
values are converted from String to respective Mongo Client types using Mongo Client API methods.mongo:maxCommitTime
is converted from String usingDuration.parse(String)
.Example of usage: